//
//  YKApproveDetailViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/4.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKApproveDetailViewController.h"
#import "AuditDetailHeaderView.h"
#import "YKClientListHeaderView.h"
#import "YKApproveDetailItem.h"
#import "YKInputMultilineView.h"
#import "YKPopupView.h"
#import "YKProcessTrackingViewController.h"
#import "YKTurnOverViewController.h"
@interface YKApproveDetailViewController ()

@end

@implementation YKApproveDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"审批详情";
    AuditDetailHeaderView *headerView = [[[NSBundle mainBundle]loadNibNamed:@"AuditDetailHeaderView" owner:self options:nil]firstObject];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    
    YKClientListHeaderView *listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"序号",@"药品名称",@"药品规格",@"采购单价"] weights:@[@1,@4,@2,@2]];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
    }];
    
    UIButton* processTrackButton = [[UIButton alloc]init];
    [self.view addSubview:processTrackButton];
    UIButton *agreeButton = [[UIButton alloc]init];
    [self.view addSubview:agreeButton];
    UIButton *disagreeButton = [[UIButton alloc]init];
    [self.view addSubview:disagreeButton];
    [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];
    [agreeButton setTitle:@"同意" forState:UIControlStateNormal];
    [disagreeButton setTitle:@"不同意" forState:UIControlStateNormal];
    [processTrackButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [agreeButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [disagreeButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [agreeButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [disagreeButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];
    agreeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    disagreeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [disagreeButton addTarget:self action:@selector(disagree:) forControlEvents:UIControlEventTouchUpInside];
    [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
    [agreeButton addTarget:self action:@selector(agree:) forControlEvents:UIControlEventTouchUpInside];
    
    [@[processTrackButton,agreeButton,disagreeButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [@[processTrackButton,agreeButton,disagreeButton] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
//    YKInputMultilineView *inputView = [[YKInputMultilineView alloc]initWithTitle:@"审批意见" input:@""];
//    [self.view addSubview:inputView];
//    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(processTrackButton.mas_top).offset(-5);
//        make.leading.equalTo(self.view.mas_leading).offset(13);
//        make.trailing.equalTo(self.view.mas_trailing).offset(-13);
//    }];
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(listHeaderView.mas_bottom);
//        make.bottom.equalTo(inputView.mas_top);
        make.bottom.equalTo(processTrackButton.mas_top);
    }];
    
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    for (int i = 0; i < 5; ++i) {
        YKApproveDetailItem *item = [YKApproveDetailItem item];
        [section addItem:item];
    }
    [self.tableViewManager addSection:section];
}

- (void)disagree:(id)sender {
    YKTurnOverViewController *vc = [YKTurnOverViewController new];
    vc.m_id = self.m_id;
    vc.type = YKTurnOverTypeDisapprove;
    vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
        [TFHTTPMANAER POST:@"workExamine/returnWorkExamine.htm" parameters:@{@"mark":text,@"businessId":businessId,@"perTask":nextId} HUD:@"转审中..." success:^(id responseObject) {
            [self showHint:@"退回成功"];
        } failure:^(id responseObject, NSError *error) {
            [self showHint:@"退回失败, 请点击重试"];
        }];
    };
    
    [YKPopupView showPopupViewWithViewController:vc];
}

- (void)processTrack:(id)sender {
    [YKPopupView showPopupViewWithViewController:[YKProcessTrackingViewController new]];
}

- (void)agree:(id)sender {
    YKTurnOverViewController *vc = [YKTurnOverViewController new];
    vc.m_id = self.m_id;
    vc.type = YKTurnOverTypeApprove;
    vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
        [TFHTTPMANAER POST:@"workExamine/completeWorkExamine.htm" parameters:@{@"mark":text,@"businessId":businessId,@"nextId":nextId} HUD:@"转审中..." success:^(id responseObject) {
            [self showHint:@"同意成功"];
        } failure:^(id responseObject, NSError *error) {
            [self showHint:@"同意失败, 请点击重试"];
        }];
    };
    
    [YKPopupView showPopupViewWithViewController:vc];
}

@end
